package com.itheima.info.manager.system.dao;

import com.itheima.info.manager.system.domain.Student;

/*
学生的 数据操作类
数组

增
删
改
查


 */
public class StudentDao implements BaseStudentDao {
    //创建 数组
    private static Student[] stus = new Student[40];
    static {
        Student student1 = new Student("00001","鹿晗",30,"1991");
        Student student2 = new Student("00002","吴签",31,"1990");
        Student student3 = new Student("00003","坤坤",26,"1995");
        Student student4 = new Student("00004","滔滔",28,"1993");
        stus[0]=student1;
        stus[1]=student2;
        stus[2]=student3;
        stus[3]=student4;

    }


    //新增  参数  学生对象   返回值  布尔类型
    // 添加到集合中
    public boolean addStudent(Student student){

        //向数组中添加数据
        //哪里有空位 添加哪里
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            //判断是否为null
            if(stu==null){
                stus[i]=student;
                return true;
            }
        }
        //返回结果
        return false;
    }

    //查询所有
    //参数   不要
    //返回值类型  所有的学生  Student[]
    public Student[] findAllStudents(){
        return stus;
    }


    public void deleteStudent(String sid){
        //根据学号 找索引的方法
        int index = getIndex(sid);
        //从数组中将元素删除  数组名[索引]=null;
        stus[index]=null;

    }
    //根据学号找索引  查
    //参数 String sid  返回值类型 int
    public  int getIndex(String sid){
        for (int i = 0; i < stus.length; i++) {
            Student stu = StudentDao.stus[i];
            if(stu!=null&&stu.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }

    public void updateStudent(String sid, Student student) {
        int index = getIndex(sid);
        stus[index]=student;
    }
}
